<?php
namespace util;


class Format
{
    /**
     * 隐藏手机中中间三位
     * @param $value 手机号
     * @return mixed
     */
    public static function mobile($value) {
        return substr_replace($value, '***', 3, 3);
    }

    public static function assignType($value) {
        $types = [
            1 => "手动分配",
            2 => "自动分配",
            3 => "自己添加",
            4 => "公海获取"
        ];

        return isset($types[$value]) ? $types [$value] : '';
    }

    public static function contactType($value) {
        $types = [
            1 => '手机号',
            2 => '微信',
        ];

        return isset($types[$value]) ? $types [$value] : '';
    }

    /**
     * 格式化时间区间
     * @param $range array 区间数组
     * @return array
     */
    public static function dateRange($range) {
        switch ($range) {
            case is_array($range):
                $start = is_string($range[0]) ? strtotime($range[0]) : $range[0];
                $end = is_string($range[1]) ? strtotime($range[1]) : $range[1];
                $end = $end + 86399;
                break;

            case strpos($range, '~') > 0:
                $arr = explode('~', $range);
                $start = $arr[0];
                $end = $arr[1];
                break;

            case strpos($range, ',') > 0:
                $arr = explode(',', $range);
                $start = strtotime($arr[0]);
                $end = strtotime($arr[1]);
                break;


            case 'd':
                $start = strtotime('today');
                $end = strtotime('tomorrow') - 1;
                break;

            case 'today':
                $start = strtotime('today');
                $end = strtotime('tomorrow') - 1;
                break;

            case 'week':
                $start = strtotime('last sunday') + 86400;
                $end = strtotime('tomorrow') - 1;
                break;

            case 'w':
                $start = strtotime('last sunday') + 86400;
                $end = strtotime('tomorrow') - 1;
                break;

            case 'month':
                $start = strtotime(date('Y-m-01'));
                $end = strtotime('tomorrow') - 1;
                break;

            case 'm':
                $start = strtotime(date('Y-m-01'));
                $end = strtotime('tomorrow') - 1;
                break;

            default:
                $start = 0;
                $end = strtotime('tomorrow') - 1;

        }

        return [$start, $end];
    }

    /**
     * 格式化分页信息
     * @param $pagination
     * @return array
     */
    public static function pagination() {
        $params = request()->param();
        $limit = isset($params['pageSize']) && $params['pageSize'] > 0 ? (int)$params['pageSize'] : 15;
        $page = isset($params['pageNum']) && $params['pageNum'] > 0 ? (int)$params['pageNum'] : 1;
        $config = [
            'list_rows' => $limit,
            'page' => $page
        ];

        return [$config, $limit, $page];
    }

    // 格式化分页数据
    public static function pgResult(&$pg) {
        $data = [
            'total' => $pg->total(),
            'limit' => $pg->listRows(),
            'page'  => $pg->currentPage(),
            'last'  => $pg->lastPage(),

        ];

        return $data;
    }
}